const GET_CURRENT_PRODUCT = 'get_current_product' //商品详情的底部弹出框的详情  初始化
const CLEAR_CURRENT_PRODUCT = 'clear_current_product' //清除当前的商品
const ADD_QUANTITY = 'add_quantity_for_current_product' //商品数量的加
const DECREASE_QUANTITY = 'decrease_quantity_for_current_product' //商品数量的减
const SET_CURRENT_FORMAT_ID = 'set_current_format_id' //设置当前商品的规格id
const SET_CURRENT_FORMAT_COLOR_ID = 'set_current_format_color_id' //设置当前商品的颜色id


const state = {
    currentProduct: null
}

const getters = {
    currentProduct: (state) => state.currentProduct
}

const mutations = {
    [GET_CURRENT_PRODUCT](state, product) { //商品详情的底部弹出框的详情  初始化
        product.quantity = 1
            //product.currentFormatId = product.defaultFormatId
        product.currentFormatId = product.format[0].productFormatId
        if (product.colors.length > 0) {
            product.currentProductColorId = product.colors[0].productColorId
        }
        state.currentProduct = product
    },
    [CLEAR_CURRENT_PRODUCT](state) { //清除当前的商品
        state.currentProduct = null
    },
    [ADD_QUANTITY](state) { //商品数量的加
        state.currentProduct.quantity += 1
    },
    [DECREASE_QUANTITY](state) { //商品数量的减
        if (state.currentProduct.quantity === 1) {
            return
        }
        state.currentProduct.quantity -= 1
    },
    [SET_CURRENT_FORMAT_ID](state, formatId) { //设置当前商品的规格id
        state.currentProduct.currentFormatId = formatId
    },
    [SET_CURRENT_FORMAT_COLOR_ID](state, formatColorId) { //设置当前商品的颜色id
        state.currentProduct.currentProductColorId = formatColorId
    }
}

const actions = {
    getCurrentProduct({ commit }, product) {
        commit(GET_CURRENT_PRODUCT, product)
    },
    clearCurrentProduct({ commit }) {
        commit(CLEAR_CURRENT_PRODUCT)
    },
    decreaseCurrentProductQuantity({ commit }) {
        commit(DECREASE_QUANTITY)
    },
    addCurrentProductQuantity({ commit }) {
        commit(ADD_QUANTITY)
    },
    changeCurrnetFormatId({ commit }, formatId) {
        commit(SET_CURRENT_FORMAT_ID, formatId)
    },
    changeCurrnetFormatColorId({ commit }, formatColorId) {
        commit(SET_CURRENT_FORMAT_COLOR_ID, formatColorId)
    }
}

export default {
    state,
    getters,
    mutations,
    actions
}